<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\api\controller;

use app\api\model\Api;
use app\api\model\JWT;
use app\api\controller\Jwt as Jwts;
use think\App;
use think\Controller;
use think\helper\Hash;
use think\Db;
use think\Session;
use app\api\model\User as UserModels;

/**
 * @title 登录
 * @description 接口说明
 */
class Userlogin extends Api
{
    /**
     * @title 发送验证码
     * @description 接口说明
     * @author 开发者
     * @url /api/userlogin/sendcode
     * @method POST

     * @param name:mobile type:string require:1 default: other: desc:手机号
     * @param name:password type:string require:1 default: other: desc:密码
     * 
     *
     */
    public function sendcode(){
        if(!input('mobile')){
            return $this->ajaxReturn(1,'请输入账号');
        }
        if(!input('password')){
            return $this->ajaxReturn(1,'请输入密码');
        }
        $mobile = input('mobile');
        $password = input('password');
        $user = db::name('user')->where('mobile',$mobile)->find();
        if(!$user){
            return $this->ajaxReturn(1,'账号错误');
        }

        if (!Hash::check((string)$password, $user['password'])) {
            return $this->ajaxReturn(1,'密码错误');
        } 

        // $mobile = trim(input('mobile'));
        $check = sys_check_mobile($user['mobiles']);
        if(!$check){
            return $this->ajaxReturn(1,'手机号格式不正确');
        }
        $res = sys_send_code($user['mobiles']);
        if($res['code'] == 'error'){
            return $this->ajaxReturn(1,$res['msg']);
        }else{
            return $this->ajaxReturn(0,'发送成功');

        }
    }
    /**
     * @title 登录接口
     * @description 接口说明
     * @author 开发者
     * @url /api/userlogin/login
     * @method POST
     * @param name:mobile type:string require:1 default: other: desc:账号
     * @param name:password type:string require:1 default: other: desc:密码
     * @param name:code type:string require:1 default: other: desc:验证码
     *
     * @return list:列表@
     * @list id:id token:token
     * 
     */
    public function login(){
        $mobile = input('mobile');
        $password = input('password');
        $code = input('code');
        $device = input('device');
        $code = trim($code);
        
        // if (!$code) {
        //     return $this->ajaxReturn(1,'验证码不能为空');
        // }
        // // //验证码验证
        // $msg_code = sys_check_code($code);
        // if (!$msg_code) {
        //     return $this->ajaxReturn(1,'验证码输入错误');
        // }
        if (!$mobile) {
            return $this->ajaxReturn(1,'请输入账号');
        }
        if (!$password) {
            return $this->ajaxReturn(1,'请输入密码');
        }
        $where[] = ['mobile','=',$mobile];
        $info = db::name('user')->where($where)->field('id,hid,username,last_login_time,avatar,mobile,role,status,num,hotel,sex,position,mobiles')->find();
        if($info){
            $UserModels = new UserModels();
            $res = $UserModels->logins($mobile,$password);
            if($res == 1){
                return $this->ajaxReturn(1,'用户不存在或被禁用！');die;
            }elseif($res == 2){
                return $this->ajaxReturn(1,'禁止访问，原因：未分配角色！');die;
            }elseif($res == 3){
                return $this->ajaxReturn(1,'禁止访问，用户所在角色未启用或禁止访问后台！');die;
            }elseif($res == 4){
                return $this->ajaxReturn(1,'密码错误！');die;
            }elseif($res == 5){
                return $this->ajaxReturn(1,'登录信息更新失败，请重新登录！');die;
            }elseif($res == 6){
                return $this->ajaxReturn(1,'未分配任何节点权限！');die;
            }elseif($res == 7){
                
                $payload=array('iss'=>'admin','iat'=>time(),'exp'=>time()+7200,'nbf'=>time(),'sub'=>'www.admin.com','jti'=>md5(uniqid('JWT').time()),'uid'=>$info['id']);
                $jwt = new Jwts();
                $token = $jwt->getToken($payload);
                $user_token = db('user_member')->where('uid',$info['id'])->where('type',1)->value('flag');
                $devices = db('user_member')->where('uid',$info['id'])->where('type',1)->value('device');
                if($user_token){
                    if($devices == $device){
                    if($user_token != $token){
                        db::name('user_member')->where('flag',$user_token)->where('type',1)->update(['flag'=>$token]);
                    }
                }
                }else{
                    $re['uid'] = $info['id'];
                    $re['flag'] = $token;
                    $re['addtime'] = time();
                    $re['device'] = $device;
                    $re['type'] = 1;
                    db::name('user_member')->insert($re);
                }
                $info['avatar'] = get_file_path($info['avatar']);
                $info['token'] = $token;
                // $getPayload_test = $jwt->verifyToken($token_test);
                return $this->ajaxReturn(0,'成功',$info);
            }
        }else{
            return $this->ajaxReturn(1,'账号错误');die;
        }
    }

}